''' 人脸检测 '''

import cv2 as cv
import numpy as np

cap = cv.VideoCapture("../resources/miku.mp4")
f_res, frame = cap.read()

net = cv.dnn.readNetFromTensorflow(model="./face_src/opencv_face_detector_uint8.pb", config="./face_src/opencv_face_detector.pbtxt")

while f_res:
    blob = cv.dnn.blobFromImage(frame, 1.0, (300, 300), [104, 117, 123], False, False)
    net.setInput(blob)
    detections = net.forward()
    for detection in detections[0, 0, :, :]:
        score = float(detection[2])
        if score > 0.5:
            left = detection[3] * frame.shape[1]
            top = detection[4] * frame.shape[0]
            right = detection[5] * frame.shape[1]
            bottom = detection[6] * frame.shape[0]
            cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (0, 255, 0), 2)

    cv.imshow("output", frame)
    if cv.waitKey(1) == ord('q'):
        break
    f_res, frame = cap.read()
